package com.cj.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cj.entity.News;
import com.cj.mapper.NewsMapper;




@Controller
public class Preheat {
	@Autowired
	RedisTemplate<String, String> redisTemplate;
	
	@Autowired
	NewsMapper newsMapper;
	
	@RequestMapping("/preInit")
	public String preInit(Model model){
		 //先从db取出 点击量数据，为了演示就简单点了  select * from news order by views desc limit 0,3  假设就取 3条，
		IPage<News> selectPage = newsMapper.selectPage(new Page<News>(0,3), new QueryWrapper<News>().orderByDesc("views"));
		List<News> newsList = selectPage.getRecords();
		String newsKey_prefix = "hnews";//新闻key的前缀
		
		redisTemplate.executePipelined(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
				redisConnection.openPipeline();
				for (News news : newsList) {
					String newsKey = newsKey_prefix + news.getNewsId();//拼凑成 一个完整的新闻key
					redisConnection.hSet(newsKey.getBytes(), "news_id".getBytes(), Long.toString(news.getNewsId()).getBytes());
					redisConnection.hSet(newsKey.getBytes(), "news_title".getBytes(), news.getNewsTitle().getBytes());
					redisConnection.hSet(newsKey.getBytes(), "views".getBytes(), Long.toString(news.getViews()).getBytes());
				}
				redisConnection.closePipeline();
				
				return null;
			}
		});	
		model.addAttribute("redis管道结束");
		return "HNews";
	}
	
	@RequestMapping("/preInit2")
	public String preInit2(Model model){
		 //先从db取出 点击量数据，为了演示就简单点了  select * from news order by views desc limit 0,3  假设就取 3条，
		IPage<News> selectPage = newsMapper.selectPage(new Page<News>(0,3), new QueryWrapper<News>().orderByDesc("views"));
		List<News> newsList = selectPage.getRecords();
		String newsKey_prefix = "hnews";//新闻key的前缀
		redisTemplate.executePipelined(new SessionCallback<Object>() {
			@Override
			public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
				for (News news : newsList) {
					Map<String,Object> DBMap = new HashMap<String, Object>();
					DBMap.put("news_id", Long.toString(news.getNewsId()));
					DBMap.put("news_title", news.getNewsTitle());
					DBMap.put("views", Long.toString(news.getViews()));
					String newsKey = newsKey_prefix + news.getNewsId();//拼凑成 一个完整的新闻key
					redisTemplate.opsForHash().putAll(newsKey, DBMap);
				}
				return null;
			}
		});
		
		model.addAttribute("redis管道结束");
		return "HNews";
	}
	
	@RequestMapping("/preInit3")
	public String preInit3(Model model){
		 //先从db取出 点击量数据，为了演示就简单点了  select * from news order by views desc limit 0,3  假设就取 3条，
		IPage<News> selectPage = newsMapper.selectPage(new Page<News>(0,3), new QueryWrapper<News>().orderByDesc("views"));
		List<News> newsList = selectPage.getRecords();
		String newsKey_prefix = "hnews";//新闻key的前缀
		
		redisTemplate.executePipelined(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
				redisConnection.openPipeline();
				for (News news : newsList) {
					String newsKey = newsKey_prefix + news.getNewsId();//拼凑成 一个完整的新闻key
					redisConnection.hSet(newsKey.getBytes(), "news_id".getBytes(), Long.toString(news.getNewsId()).getBytes());
					redisConnection.hSet(newsKey.getBytes(), "news_title".getBytes(), news.getNewsTitle().getBytes());
					//这好比 zadd newsclick xx(点击量) news101
		            //集合名 我们定位 newsclick 。代表新闻点击量集合。 因为万一后面 还有 新闻评论数、点赞数呢？
					redisConnection.zAdd("newsclick".getBytes(), Long.valueOf(news.getViews()), ("news_id"+news.getNewsId()).getBytes());
				}
				redisConnection.closePipeline();
				
				return null;
			}
		});	
		model.addAttribute("redis管道结束");
		return "HNews";
	}
	
}
